package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponse;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 品牌管理
 */
@Controller
@RequestMapping("brand")
public class BrandController {

    @Reference
    private BrandServiceI brandServiceI;
    @RequestMapping("showBrand")
    public String showBrand(){
        return "admin/brand";
    }

    /*
    *  public PageInfo<Blog> ajaxBlog(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogList = blogMapper.selectByExample(new BlogExample());
        PageInfo<Blog> pageInfo = new PageInfo<Blog>(blogList);
        return pageInfo;
    }
    * */
    @RequestMapping("listBrands")
    @ResponseBody
    public Result<List<TbBrand>> listBrands(){
        try {
            List<TbBrand> brandList=brandServiceI.listBrands();
            return Result.jsonResponse(HttpResponseEnum.OK).setData(brandList);
        }catch (Exception e){
           return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand brand){
        try {
          int brand1=brandServiceI.addBrand(brand);
          if (brand1 > 0){
              return Result.jsonResponse(HttpResponseEnum.OK);
          }else {
              return Result.jsonResponse(HttpResponseEnum.FAIL);
          }
        }catch (Exception e){
            return Result.jsonResponse(HttpResponseEnum.UNATUTH);
        }
    }

    @RequestMapping("deleteBrand")
    @ResponseBody
    public Result deleteBrand(@RequestParam("ids[]")Integer[] ids){
        try {
            int getId=brandServiceI.deleteBrand(ids);
            if (getId>0){
                return Result.jsonResponse(HttpResponseEnum.OK);
            }else {
                return Result.jsonResponse(HttpResponseEnum.FAIL);
            }
        }catch (Exception e){
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("getBrandById/{id}")
    @ResponseBody
    public Result<TbBrand> getBrandById(@PathVariable("id")Long id){
        try {
            TbBrand tbBrand=brandServiceI.getBrandById(id);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(tbBrand);
        }catch (Exception e){
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("updateBrand")
    @ResponseBody
    public Result updateBrand(TbBrand tbBrand){
        try {
            int brand=brandServiceI.updateBrand(tbBrand);
            if (brand>0){
                return Result.jsonResponse(HttpResponseEnum.OK);
            }else {
                return Result.jsonResponse(HttpResponseEnum.FAIL);
            }
        }catch (Exception e){
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
}
